import request from '@/utils/request'

// 查询收藏列表
export function listFavorite(query) {
  return request({
    url: '/getAllFavorites',
    method: 'get'
  })
}

// 查询收藏详细信息
export function getFavorite(id) {
  return request({
    url: '/getFavorite/' + id,
    method: 'get'
  })
}

// 新增收藏
export function addFavorite(data) {
  return request({
    url: '/insertFavorite',
    method: 'post',
    data: data
  })
}

// 删除收藏
export function deleteFavorite(id) {
  return request({
    url: '/deleteFavorite/' + id,
    method: 'get'
  })
}

// 批量删除收藏
export function batchDeleteFavorite(ids) {
  const idArray = ids.split(',');
  const promises = idArray.map(id => deleteFavorite(id));
  
  return Promise.all(promises).then(() => {
    return {
      code: 200,
      message: 'success',
      data: null
    }
  });
}

// 导出收藏数据
export function exportFavorite(query) {
  return request({
    url: '/exportFavorites',
    method: 'get',
    params: query,
    responseType: 'blob'
  })
}

// 获取用户的收藏列表
export function getUserFavorites(userId) {
  return request({
    url: '/getUserFavorites/' + userId,
    method: 'get'
  })
}

// 检查用户是否收藏了某辆车
export function checkFavorite(userId, carId) {
  return request({
    url: '/checkFavorite/' + userId + '/' + carId,
    method: 'get'
  })
}

// 获取车辆的收藏数量
export function getCarFavoriteCount(carId) {
  return request({
    url: '/getCarFavoriteCount/' + carId,
    method: 'get'
  })
} 